package ihm;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


import job.UserOption;

/**
 * Classe permettant d'affecter une action � un �l�ment
 * @author Laurent
 */
public class Listener implements ActionListener, UserOption {
	private int initLvl = 1;
	private int xSize = 10;
	private int ySize = 20;
	private float userPas = 40;
	/**
	 * D�finit l'action correspondant � l'�l�ment o� l'utilisateur a int�ragi
	 */
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == Content.preferences) {
				createOptionWindow();
		}
		else if (e.getSource() == Content.help) {
		}
		else if (e.getSource() == Content.about) {
			String str = "Ce jeu a �t� con�u par Flavien Busseuil et Laurent Muthelet dans le cadre d'un projet scolaire.";
			JOptionPane.showMessageDialog(new JPanel(),str,"A propos de...",JOptionPane.PLAIN_MESSAGE);
		}
		else if (e.getSource() == Content.exit) {
			Content.mainWindow.dispose();
		}
		else if (e.getSource() == Content.ok) {
			Content.frame.dispose();
		}
		if (e.getSource() == Content.ok) {
			
		}
    }
	
	/**
	 * Cr�ation de la fen�tre d'options
	 */
	public void createOptionWindow(){
		Content.panel.removeAll();
		Content.list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		Content.panel.add(new JLabel("Niveau de jeu :"));
		Content.ok.addActionListener(this);
		
		// lvl
		ArrayList<Integer> timerValues = new ArrayList<Integer>();
		timerValues.add(1);
		timerValues.add(2);
		timerValues.add(3);
		timerValues.add(4);
		timerValues.add(5);
		timerValues.add(6);
		timerValues.add(7);
		timerValues.add(8);
		timerValues.add(9);
		timerValues.add(10);
		DefaultComboBoxModel model = new DefaultComboBoxModel(timerValues.toArray());
        JComboBox combo = new JComboBox(model);
        combo.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                        Object o = ((JComboBox)ae.getSource()).getSelectedItem();
                        Integer t = (Integer) o;
                        initLvl = t;
                }
                
        });
        
        // size
        ArrayList<Integer> sizeValues = new ArrayList<Integer>();
        sizeValues.add(5);
        sizeValues.add(6);
        sizeValues.add(7);
        sizeValues.add(8);
        sizeValues.add(9);
        sizeValues.add(10);
        sizeValues.add(12);
        sizeValues.add(14);
        sizeValues.add(16);
        sizeValues.add(18);
        sizeValues.add(20);
        // xSize
        DefaultComboBoxModel model2 = new DefaultComboBoxModel(sizeValues.toArray());
		JComboBox comboXSize = new JComboBox(model2);
		comboXSize.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                        Object o = ((JComboBox)ae.getSource()).getSelectedItem();
                        xSize = (Integer) o;
                }
        });
        
        // ySize
		DefaultComboBoxModel model3 = new DefaultComboBoxModel(sizeValues.toArray());
        JComboBox comboYSize = new JComboBox(model3);
        comboYSize.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                        Object o = ((JComboBox)ae.getSource()).getSelectedItem();
                        ySize = (Integer) o;
                }
        });
        comboXSize.setSelectedItem(xSize);
        comboYSize.setSelectedItem(ySize);
        combo.setSelectedItem(initLvl);
        
        Content.panel.add(combo);
        Content.panel.add(new JLabel("Taille du terrain horizontale :"));
        Content.panel.add(comboXSize);
        Content.panel.add(new JLabel("Taille du terrain verticale :"));
        Content.panel.add(comboYSize);
		Content.panel.add(Content.ok);
		Content.frame.add(Content.panel);
		Content.frame.setSize(250, 180);
		Content.frame.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width*3/8, Toolkit.getDefaultToolkit().getScreenSize().height*3/8);
		Content.frame.show();
	}
	/** Retourne le niveau initiale choisi pour la partie prochaine */
	public int getInitLvl() {
		return initLvl;
	}
	/** Retourne la dimension x du playground choisi */
	public int getPlayGroundX() {
		return xSize;
	}
	/** Retourne la dimension y du playground choisi */
	public int getPlayGroundY() {
		return ySize;
	}
	/** Retourne la valeur du pas du timer */
	public float getTimerPas() {
		return userPas;
	}
}
